import router from "./router";
import store from "./store";


router.beforeEach(async (to, from, next) => {
    if (store.getters.token) {
        if (to.path === '/login') {
            next('/')
        } else {
            if (!store.getters.name) {
                // 刷新进入页面时,没有数据,应该获取
                // 但是如果已经在页面里面, 在进行跳转
                // 因为之前数据已经那个, 就不需要再拿
                await store.dispatch('user/getUserInfo')
                const res = await store.dispatch('permission/filterRoutes', store.getters.menus)
                router.addRoutes(
                    [
                        ...res,
                        { path: '*', redirect: '/404', hidden: true }
                    ]
                )
                next(to.path)
                return
            }
            next()
        }
    } else {
        const widthList = ['/login', '/404', '/test']
        if (widthList.indexOf(to.path) > -1) {
            next()
        } else {
            next('/login')
        }
    }
})



